package com.itheima.ticket.ticket04;

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

public class Ticket implements Runnable{
  private int ticket=100;
  //创建锁对象
    private Lock lock= new ReentrantLock();
    @Override
    public void run() {
       while (true){
           //人有操作  耗时间
           // 加锁
           lock.lock();
           try {
               if(ticket>0){
                 Thread.sleep(10);

                   System.out.println(Thread.currentThread().getName()+"正在卖"+ticket+"张票");
                   ticket--;
               }else {
                   break;
               }
           } catch (InterruptedException e) {
               e.printStackTrace();
           } finally {
               //释放锁
               lock.unlock();

           }


       }
    }
}
